package com.group3.group3.user.service.impl;

import com.group3.group3.login.entity.MenuLevelEntity;
import com.group3.group3.mybatis.entity.PtMenuEntity;
import com.group3.group3.mybatis.entity.PtUserEntity;
import com.group3.group3.mybatis.flexigrid.PadingRstType;
import com.group3.group3.mybatis.flexigrid.PagingBean;
import com.group3.group3.mybatis.ztree.ZtreeEntity;
import com.group3.group3.spring.security.GdmtUserEntity;
import com.group3.group3.user.entity.UserSearchEnity;
import com.group3.group3.user.repository.UserDao;
import com.group3.group3.user.service.UserService;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service("userService")
public class UserServiceImpl implements UserService {
    private final static Integer MENU_FIRST_LEVEL = 1;
    @Resource
    private UserDao userDao;

    @Override
    public PtUserEntity queryUserInfoByUserName(String username) {
        return userDao.queryUserInfoByUserName(username);
    }

    @Override
    public List<MenuLevelEntity> queryMenuInfoByLang(String lang) {

        GdmtUserEntity gdmtUserEntity = (GdmtUserEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        List<MenuLevelEntity> levelEntities = new ArrayList<>();
        List<PtMenuEntity> ptMenuEntities = userDao.queryMenuInfoByLang(gdmtUserEntity.getUserUuid(),lang);
        MenuLevelEntity menuLevelEntity1 = null;
        MenuLevelEntity menuLevelEntity2 = null;
        MenuLevelEntity menuLevelEntity3 = null;
        for(PtMenuEntity ptMenuEntity1: ptMenuEntities){
            if(MENU_FIRST_LEVEL.equals(ptMenuEntity1.getMenuLevel())){
                menuLevelEntity1 = new MenuLevelEntity(ptMenuEntity1);
                levelEntities.add(menuLevelEntity1);
                for(PtMenuEntity ptMenuEntity2: ptMenuEntities){
                    if(ptMenuEntity1.getMenuId().equals(ptMenuEntity2.getParentId())){
                        menuLevelEntity2 = new MenuLevelEntity(ptMenuEntity2);
                        menuLevelEntity1.addList(menuLevelEntity2);
                        for(PtMenuEntity ptMenuEntity3: ptMenuEntities){
                            if(ptMenuEntity2.getMenuId().equals(ptMenuEntity3.getParentId())){
                                menuLevelEntity3 = new MenuLevelEntity(ptMenuEntity3);
                                menuLevelEntity2.addList(menuLevelEntity3);
                            }
                        }
                    }

                }
            }
        }

        return levelEntities;
    }

    @Override
    public List<String> queryMenuInfoList(String userUuid) {
        Locale locale = LocaleContextHolder.getLocale();
        return userDao.queryMenuInfoList(userUuid);
    }

    @Override
    public void addUser(String uuid,String userNick, String userName, String password, String mobile, String email) {
        userDao.addUser(uuid,userNick,userName,password,mobile,email);
    }

    @Override
    public void updateUser(String uuid,String userNick, String userName, String password, String mobile, String email) {
        userDao.updataUser(uuid,userNick,userName,password,mobile,email);
    }

    @Override
    public void deleteUser(String uuid) {
        userDao.deleteUser(uuid);
    }

    @Override
    public PadingRstType<PtUserEntity> getUserInfolistByPage(UserSearchEnity search, PagingBean paging) {
        paging.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        List<PtUserEntity> list = userDao.queryUserListByPadding(search,paging);
        Integer total = userDao.queryUserListTotal(search);
        padingRstType.setRawRecords(list);
        padingRstType.setTotal(total);
        padingRstType.putItems();
        return padingRstType;
    }

    @Override
    public void deleteUserById(String userUuid) {
        userDao.deleteUserById(userUuid);
    }
    @Resource
    private MessageSource messageSource;
    @Override
    public List<ZtreeEntity> getRoleZtreeListByUserId(String userUuid) {
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr =messageSource.getMessage("user.role.info",null,locale);

        List<ZtreeEntity> list = userDao.getRoleZtreeList();

        List<String> roleUUuidList = userDao.queryRoleRefUserByUserId(userUuid);
        for(ZtreeEntity item:list){
            for(String roleUuid :roleUUuidList){
                if(roleUuid.equals(item.getId())){
                    item.setChecked(true);
                }
            }
        }
        ZtreeEntity root = new ZtreeEntity();
        root.setId("0");
        root.setpId("-1");
        root.setName(msgStr);
        list.add(root);
        return list;
    }

    @Override
    public void saveRoleByUserId(String userUuid, String roleUuids) {
        String[] roleUuidArray = roleUuids.split(",");
        userDao.deleteUserRefRole(userUuid);
        for(String roleUuid: roleUuidArray){
            userDao.insertUserRefRole(userUuid,roleUuid);
        }
        
    }
}
